<!--
 * @Description: 我的购物车页面组件
 * @Author: pdx
 * @Date: 2021-11-30 01:55:47
 * @LastEditors: pdx
 * @LastEditTime: 2021-11-30 13:36:42
 -->

<template>
  <div class="shoppingCart">
    <!-- 购物车头部 -->
    <div class="cart-header">
      <div class="cart-header-content">
        <p>
          <i class="el-icon-shopping-cart-full" style="color:#ff6700; font-weight: 600;"></i>
          我的购物车
        </p>
        <span>温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</span>
      </div>
    </div>
    <!-- 购物车头部END -->

    <!-- 购物车主要内容区 -->
    <div class="content" v-if="cartList.length > 0">
      <el-table
          :data="cartList"
          tooltip-effect="dark"
          style="width: 100%;"
          :header-row-style="{height: '80px',fontSize: '20px'}"
          :row-style="{height: '85px'}"
          @selection-change="changeCheckAll">

        <el-table-column type="selection" width="55" label="全选"/>

        <el-table-column width="200">
          <template slot-scope="scope">
            <router-link :to="{ path: '/game/details', query: {id:scope.row.gameId} }" target="_blank">
              <img style="width : 130px; height: 85px; border-radius: 10px" :src="scope.row.gameDefImg"/>
            </router-link>
          </template>
        </el-table-column>

        <el-table-column label="游戏名称" prop="gameName" width="400">
          <template slot-scope="scope">
            <router-link :to="{ path: '/game/details', query: {id:scope.row.gameId} }" target="_blank">
              {{ scope.row.gameName }}
            </router-link>
          </template>
        </el-table-column>

        <el-table-column label="单价">
          <template slot-scope="scope">
            <span style="font-size: 0.8rem; color: #b0b0b0" v-if="scope.row.seckillPrice == null">
                ￥{{ scope.row.price.toFixed(2) }}
            </span>
            <span style="font-size: 0.8rem; color: #b0b0b0; text-decoration: line-through;" v-else>
                ￥{{ scope.row.price.toFixed(2) }}
            </span>
            <span v-if="scope.row.seckillPrice != null" style="color: #f25807">
              ￥{{ scope.row.seckillPrice.toFixed(2) }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-popover placement="right">
              <p>确定删除吗？</p>
              <div style="text-align: right; margin: 10px 0 0">
                <el-button
                    type="primary"
                    size="mini"
                    @click="deleteCart($event,scope.row.gameId)"
                >确定
                </el-button>
              </div>
              <i class="el-icon-error" slot="reference" style="font-size: 18px; color: red"></i>
            </el-popover>
            <p v-if="scope.row.isWished == 0">
              <span style="cursor: pointer;" :id="scope.row.gameId" @click="addWish(item)">
                加入心愿单
              </span>
              <span style="color: #f25807; display: none" :id="'have-' + scope.row.gameId">
                已加入心愿单
              </span>
            </p>
            <p v-else style="color: #f25807">
              已加入心愿单
            </p>
          </template>
        </el-table-column>
      </el-table>
      <div style="height:20px;background-color: #f5f5f5"></div>
      <!-- 购物车底部导航条 -->
      <div class="cart-bar">
        <div class="cart-bar-left">
          <span>
            <router-link style="color: #2a80ef" to="/games" target="_blank">继续购物</router-link>
          </span>
          <span class="sep">|</span>
          <span class="cart-total">
            共
            <span class="cart-total-num">{{ getNum }}</span> 款游戏，已选择
            <span class="cart-total-num">{{ checkNum }}</span> 件
          </span>
        </div>
        <div class="cart-bar-right">
          <span>
            <span class="total-price-title">合计：</span>
            <span class="total-price">{{ totalPrice }} 元</span>
          </span>
          <router-link :to="checkNum > 0 ? '/confirmOrder' : ''" target="_blank">
            <div :class="checkNum > 0 ? 'btn-primary' : 'btn-primary-disabled'">去结算</div>
          </router-link>
        </div>
      </div>
      <!-- 购物车底部导航条END -->
    </div>
    <!-- 购物车主要内容区END -->

    <!-- 购物车为空的时候显示的内容 -->
    <div v-else class="cart-empty">
      <div class="empty">
        <h2>您的购物车还是空的！</h2>
        <router-link to="/games">
          <p>快去购物吧！</p>
        </router-link>
      </div>
    </div>
    <!-- 购物车为空的时候显示的内容END -->
  </div>
</template>
<script>
import {mapActions} from "vuex";
import {mapGetters} from "vuex";
import gameCart from "@/api/cart/gameCart";
import {BigNumber} from 'bignumber.js';
import {setGameCart} from "@/utils/cart";

export default {
  data() {
    return {
      cartList: [],
      memberWish: {}, //心愿单
      checks: [],
      checkNum: 0, //被选中的游戏
      totalPrice: 0,
      page: 1,  //当前页码
      limit: 10,  // 每页显示的商品数量
      total: 0,  // 商品总量
    };
  },

  computed: {
    ...mapGetters([
      "getShoppingCart",
      "getNum",
      "getCheckGameIds"
    ]),
  },

  created() {
    if (!this.$store.getters.getUser) {

    } else {
      //获取用户购物车列表
      this.getCart();
    }
  },

  methods: {
    ...mapActions(["updateShoppingCart", "deleteShoppingCart", "setCheckGameIds"]),

    getCart() {
      gameCart.getGameCartListInfo()
          .then(result => {
            const data = result.data
            this.cartList = data
          })
    },

    //添加心愿单
    addWish(game) {
      this.memberWish.gameId = game.gameId
      this.memberWish.gameName = game.gameName
      this.memberWish.gameDefImg = game.gameDefImg
      this.memberWish.memberId = this.$store.getters.getUser.id

      gameCart.addWishInfo(this.memberWish)
          .then(result => {
            let noShowId = document.getElementById(game.gameId)
            noShowId.style.display = 'none'

            let showId = document.getElementById("have-" + game.gameId)
            showId.style.display = ''
          })

    },

    //选中
    changeCheckAll(val) {
      if (val.length > 0) {
        this.totalPrice = 0;
        this.checks = []
        for (let i = 0; i < val.length; i++) {
          this.checks.push(val[i].gameId);
          let price = BigNumber(val[i].seckillPrice == null ? val[i].price : val[i].seckillPrice)
          this.totalPrice = BigNumber(this.totalPrice)
          this.totalPrice = price.plus(this.totalPrice).toNumber()
        }
      } else {
        this.checks = []
        this.totalPrice = BigNumber(0)
      }
      // this.setCheckGameIds(this.checks)
      setGameCart(this.checks)
      this.checkNum = val.length
    },

    deleteCart(e, gameId) {
      gameCart.deleteCartInfo(gameId)
          .then(result => {
            this.getCart()
          })
    }
  },
};
</script>
<style scoped>
.shoppingCart {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}

/* 购物车头部CSS */
.shoppingCart .cart-header {
  height: 64px;
  border-bottom: 2px solid #ff6700;
  background-color: #fff;
  margin-bottom: 20px;
}

.shoppingCart .cart-header .cart-header-content {
  width: 1225px;
  margin: 0 auto;
}

.shoppingCart .cart-header p {
  font-size: 28px;
  line-height: 58px;
  float: left;
  font-weight: normal;
  color: #424242;
}

.shoppingCart .cart-header span {
  color: #757575;
  font-size: 12px;
  float: left;
  height: 20px;
  line-height: 20px;
  margin-top: 30px;
  margin-left: 15px;
}

/* 购物车头部CSS END */

/* 购物车主要内容区CSS */
.shoppingCart .content {
  width: 1225px;
  margin: 0 auto;
  background-color: #fff;
}

/* 购物车表头及CSS END */

/* 购物车底部导航条CSS */
.shoppingCart .cart-bar {
  width: 1225px;
  height: 50px;
  line-height: 50px;
  background-color: #fff;
}

.shoppingCart .cart-bar .cart-bar-left {
  float: left;
}

.shoppingCart .cart-bar .cart-bar-left a {
  line-height: 50px;
  margin-left: 32px;
  color: #757575;
}

.shoppingCart .cart-bar .cart-bar-left a:hover {
  color: #ff6700;
}

.shoppingCart .cart-bar .cart-bar-left .sep {
  color: #eee;
  margin: 0 20px;
}

.shoppingCart .cart-bar .cart-bar-left .cart-total {
  color: #757575;
}

.shoppingCart .cart-bar .cart-bar-left .cart-total-num {
  color: #ff6700;
}

.shoppingCart .cart-bar .cart-bar-right {
  float: right;
}

.shoppingCart .cart-bar .cart-bar-right .total-price-title {
  color: #ff6700;
  font-size: 14px;
}

.shoppingCart .cart-bar .cart-bar-right .total-price {
  color: #ff6700;
  font-size: 30px;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary {
  float: right;
  width: 200px;
  text-align: center;
  font-size: 18px;
  margin-left: 50px;
  background: #ff6700;
  color: #fff;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary-disabled {
  float: right;
  width: 200px;
  text-align: center;
  font-size: 18px;
  margin-left: 50px;
  background: #e0e0e0;
  color: #b0b0b0;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary:hover {
  background-color: #f25807;
}

/* 购物车底部导航条CSS END */
/* 购物车主要内容区CSS END */

/* 购物车为空的时候显示的内容CSS */
.shoppingCart .cart-empty {
  width: 1225px;
  margin: 0 auto;
}

.shoppingCart .cart-empty .empty {
  height: 300px;
  padding: 0 0 130px 558px;
  margin: 65px 0 0;
  background: url(../assets/imgs/cart-empty.png) no-repeat 124px 0;
  color: #b0b0b0;
  overflow: hidden;
}

.shoppingCart .cart-empty .empty h2 {
  margin: 70px 0 15px;
  font-size: 36px;
}

.shoppingCart .cart-empty .empty p {
  margin: 0 0 20px;
  font-size: 20px;
}

/* 购物车为空的时候显示的内容CSS END */
</style>